<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\initialize;
use app\admin\model\Gift as Gifts;
use think\File;

class Gift extends Controller
{
    private $gift;

    public function _initialize()
    {
        parent::_initialize();
        $this->gift = new Gifts;
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $data = request()->get();
        $where = '%%';
        if (array_key_exists('search', $data)) {
            $where = '%'.$data['search'].'%';
        }
        $list = $this->gift->indexdata($where);
        return view('gift/index', ['list' => $list]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return view('gift/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save()
    {
        $data = request()->post();
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('icon');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            $data['icon'] = $info->getSaveName();
            $res = $this->gift->savedata($data);
        } else {
            $this->error('上传失败, 请重试');
        }
        $this->success('上传成功', 'admin/Gift/index');
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $res = $request->put();
        $data['id'] = $res['id'];
        $data['name'] = $res['pname'];
        $data['price'] = $res['pprice'];
        $data['icon'] = $res['picon'];
        $res = $this->gift->updatedata($data);
        if ($res) {
            return json($data);
        }
        return false;
    }

    /**
     * 更改指定资源status
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        // 更新礼物表中信息
        $data['id'] = $id;
        $data['status'] = 2;
        $res = $this->gift->updatedata($data);
        if ($res > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'id为: ' . $id . '的用户删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'id为: ' . $id . '的用户删除失败!请重试!';
        }

        return json($info);
    }

    public function pic()
    {
        $file = request()->file()['file'];
        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            $data['icon'] = $info->getSaveName();
        } else {
            return false;
        }
        return json($data['icon']);
    }

    public function ban()
    {
        $data = request()->post();
        $data['status'] = $data['status'] == '0'?'1':'0';
        $res = $this->gift->updatedata($data);
        if ($res) {
            return $data;
        }
        return false;
    }

    public function trash()
    {
        $data = request()->get();
        $where = '%%';
        if (array_key_exists('search', $data)) {
            $where = '%'.$data['search'].'%';
        }
        $list = $this->gift->trashdata($where);
        return view('gift/trash', ['list' => $list]);
    }

    public function del()
    {
        $id = request()->post()['id'];
        $res = $this->gift->del($id);
        if ($res) {
            return true;
        }
        return false;
    }

    public function reset()
    {
        $data = request()->post();
        $data['status'] = '0';
        $res = $this->gift->updatedata($data);
        if ($res) {
            return $data;
        }
        return false;
    }

}
